/// 问答回答数据模型
/// 包含用户对问题的回答信息
class AnswerEntity {
  final String answerId;
  final String questionId;
  final String authorId; // 回答者ID
  final String content;
  final DateTime answerTime;
  final int likeCount;
  final List<String> likedUserIds; // 点赞用户ID列表

  const AnswerEntity({
    required this.answerId,
    required this.questionId,
    required this.authorId,
    required this.content,
    required this.answerTime,
    this.likeCount = 0,
    this.likedUserIds = const [],
  });

  /// 从JSON创建回答对象
  factory AnswerEntity.fromJson(Map<String, dynamic> json) {
    return AnswerEntity(
      answerId: json['answerId'] as String,
      questionId: json['questionId'] as String,
      authorId: json['authorId'] as String,
      content: json['content'] as String,
      answerTime: DateTime.parse(json['answerTime'] as String),
      likeCount: json['likeCount'] as int? ?? 0,
      likedUserIds:
          (json['likedUserIds'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'answerId': answerId,
      'questionId': questionId,
      'authorId': authorId,
      'content': content,
      'answerTime': answerTime.toIso8601String(),
      'likeCount': likeCount,
      'likedUserIds': likedUserIds,
    };
  }

  /// 复制回答对象并修改部分属性
  AnswerEntity copyWith({
    String? answerId,
    String? questionId,
    String? authorId,
    String? content,
    DateTime? answerTime,
    int? likeCount,
    List<String>? likedUserIds,
  }) {
    return AnswerEntity(
      answerId: answerId ?? this.answerId,
      questionId: questionId ?? this.questionId,
      authorId: authorId ?? this.authorId,
      content: content ?? this.content,
      answerTime: answerTime ?? this.answerTime,
      likeCount: likeCount ?? this.likeCount,
      likedUserIds: likedUserIds ?? this.likedUserIds,
    );
  }

  @override
  String toString() {
    return 'AnswerEntity(answerId: $answerId, questionId: $questionId, authorId: $authorId)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is AnswerEntity && other.answerId == answerId;
  }

  @override
  int get hashCode => answerId.hashCode;
}
